﻿using System;
using System.Text.RegularExpressions;
using UnityEngine;
using UnityEngine.UI;
#if UNITY_EDITOR
using UnityEditor;
#endif

namespace Jinndev.Localization {

    [RequireComponent(typeof(Text))]
    public class LocalizationText : MonoBehaviour, ILocalizable {

        [Tooltip("是否使用Text的文本初始化format")]
        public bool initWithText = true;
        [Tooltip("初始化时是否包装text为正则形式")]
        public bool wrapInitText = false;
        [Tooltip("正则形式的格式文本")]
        public string format;
        [Tooltip("格式文本的参数")]
        public string[] args;

        private Text text;

        private void Awake() {
            LocalizationManager.RegisterLocalizable(this);
            text = GetComponent<Text>();
        }

        private void OnDestroy() {
            LocalizationManager.UnregisterLocalizable(this);
        }

        private void Start() {
            if (text != null && initWithText) {
                format = wrapInitText ? LocalizationUtil.WrapRegex(text.text) : text.text;
            }
            RefreshText();
        }

        public void SetText(string format, params object[] args) {
            this.format = format;
            this.args = new string[args == null ? 0 : args.Length];
            for (int i = 0; i < args.Length; i++) {
                this.args[i] = args[i] == null ? null : args[i].ToString();
            }

            if (text == null) {
                text = GetComponent<Text>();
            }
            if (text != null) {
                try {
                    if (args == null || args.Length == 0) {
                        text.text = LocalizationUtil.TranslateRegex(format);
                    }
                    else {
                        text.text = string.Format(LocalizationUtil.TranslateRegex(format), args);
                    }
                }
                catch (Exception e) {
                    Debug.LogException(e);
                }
            }
        }

        public void RefreshText() {
            if (!string.IsNullOrWhiteSpace(format)) {
                SetText(format, args);
            }
        }

    }


#if UNITY_EDITOR
    [CustomEditor(typeof(LocalizationText))]
    [CanEditMultipleObjects]
    public class LocalizationTextEditor : Editor {

        private SerializedProperty initWithText;
        private SerializedProperty wrapInitText;
        private SerializedProperty format;
        private SerializedProperty args;

        private void OnEnable() {
            initWithText = serializedObject.FindProperty("initWithText");
            wrapInitText = serializedObject.FindProperty("wrapInitText");
            format = serializedObject.FindProperty("format");
            args = serializedObject.FindProperty("args");
        }

        public override void OnInspectorGUI() {
            serializedObject.Update();
            EditorGUILayout.PropertyField(initWithText);
            if (initWithText.boolValue) {
                EditorGUILayout.PropertyField(wrapInitText);
            }
            else {
                EditorGUILayout.PropertyField(format);
            }
            EditorGUILayout.PropertyField(args);
            serializedObject.ApplyModifiedProperties();
        }

    }
#endif

}
